<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2014/10/15
 * Time: 16:24
 */
class AdminController extends Controller{
    protected $project_id;
    protected $limit = 15;
    //ajax返回格式 0 失败  1成功
    protected $ajaxreturn = array('status' => 0,'note' => '', 'data' => array());
    public $menu = array(
        1 => array(
            'name' => '系统设置',
            'url' => '/webadmin/index/index',
            'controller' => array('user', 'group','index'),
            'sub' => array(
                101 => array(
                    'name' => '个人信息',
                    'url' => '/webadmin/user/change',
                    'action' => array('user.change'),
                ),
                201 => array(
                    'name' => '管理员列表',
                    'url' => '/webadmin/user/list',
                    'action' => array('user.list','user.add','user.doadd','user.edit','user.doedit'),
                ),
                202 => array(
                    'name' => '用户组管理',
                    'url' => '/webadmin/group/list',
                    'action' => array('group.list','group.add','group.doadd','group.edit','group.doedit','group.del',
                        'group.auth','group.doauth'),
                )
            ),
        ),
        5 => array(
            'name' => '项目管理',
            'url' => '/webadmin/network/bank',
            'controller' => array('project','network','question','progress','netquestion','report'),
            'sub' => array(
                501 => array(
                    'name' => '银行管理',
                    'url' => '/webadmin/network/bank',
                    'action' => array('network.bank','network.addbank','network.doaddbank','network.editbank','network.doeditbank'),
                ),
                503 => array(
                    'name' => '网点信息管理',
                    'url' => '/webadmin/network/list',
                    'action' => array('network.list','network.add','network.edit','network.import')
                ),
                504 => array(
                    'name' => '项目信息管理',
                    'url' => '/webadmin/project/list',
                    'action' => array('project.list','project.add','project.edit')
                ),
                505 => array(
                    'name' => '问卷管理',
                    'url' => '/webadmin/question/list',
                    'action' => array('question.list','question.set','question.add','question.edit','question.answerimport',
                        'question.import','question.attachment','netquestion.list','question.subjectedit')
                ),
                506 => array(
                    'name' => '项目进度查询',
                    'url' => '/webadmin/progress/schedule',
                    'action' => array('progress.schedule','progress.projectquestion','progress.netquestion')
                ),
                507 => array(
                    'name' => '附件管理',
                    'url' => '/webadmin/question/zip',
                    'action' => array('question.zip')
                ),
                508 => array(
                    'name' => '报备管理',
                    'url' => '/webadmin/report/index',
                    'action' => array('report.index')
                ),
            ),
    	),
        6 => array(
            'name' => '人员排班填报',
            'url' => '/webadmin/staff/network',
            'controller' => array('staff'),
            'sub' => array(
                601 => array(
                    'name' => '排班网点',
                    'url' => '/webadmin/staff/network',
                    'action' => array('staff.network','staff.list','staff.add')
                )
            ),
        ),


    );

    /**
     * 初始化
     */
    public function init(){
        $this->smarty = Yii::app()->smarty;
        $this->smarty->template_dir = Yii::getPathOfAlias('application.modules').'/webadmin/views';
        $this->smarty->assign('menu', $this->menu);   
       // print_r($this->menu);exit;
    }
    protected $username = '';
    protected $user_id = '';
    protected $group_id = '';
    protected $protect_id = 0;
    public function beforeAction($action) {
        if (!Yii::app()->session['user_id']) {
             $this->jump('请登陆后操作','/webadmin/login');
        }
        //判断是否登录
        parent::beforeAction($action);
        $controller = strtolower($this->getId());
        $action = strtolower($action->id);
        
        //判断是否有权限
        if (!AdminAuthIdentity::checkauth($controller, $action)){
            if (Yii::app()->request->isAjaxRequest) {
                $this->setreturn(0, '无权限，请联系管理员');
                $this->_ajax();
            } else {
                $this->jump('无权限，请联系管理员');
            } 
        }

        //echo $controller;  
        $this->smarty->assign('controller', $controller);
        $this->smarty->assign('current_action', $controller.'.'.$action);
        $this->username = Yii::app()->session['username'];
        $this->smarty->assign('username', $this->username);
        $this->user_id = Yii::app()->session['user_id'];
        $this->group_id = Yii::app()->session['group_id'];
        $this->project_id = Yii::app()->session['project_id'];
        $this->smarty->assign('group_auth', Yii::app()->session['group_auth']);
        return true;
    }   
    /**
     * js跳页
     */
    protected function jump($msg, $url=''){
        if ($url) {
            if (stripos($url, 'http://')!==false) {
                echo '<script>alert("'.$msg.'");window.location.href="'.$url.'"</script>';exit;
            } else {
                echo '<script>alert("'.$msg.'");window.location.href="'.Yii::app()->createUrl($url).'"</script>';exit;
            }
        } else {//没有返回上一级
            echo '<script>alert("'.$msg.'");history.go(-1);</script>';exit;
        }
    }
    protected function _ajax($arr=array()){
        if ($arr) {
            $this->ajaxreturn = $arr;
        }
        if (isset($_GET['callback'])) {
            echo $_GET['callback'] . '(' .CJSON::encode($this->ajaxreturn).')';exit;
        } else {
            echo CJSON::encode($this->ajaxreturn);exit;
        }
    }
    /**
     * 设置ajaxreturn
     */
    protected function setreturn($status=0, $note='', $data=array()){
        $this->ajaxreturn = array('status' => $status,'note' => $note, 'data' => $data);
    }
}